//
//  ZWLLiveViewController.m
//  zwlzhibo
//
//  Created by czh on 16/9/2.
//  Copyright © 2016年 czh. All rights reserved.
//

#import "ZWLLiveViewController.h"
#import <IJKMediaFramework/IJKMediaFramework.h>
#import "ZWLZBModel.h"
#import "ZWLItemModel.h"
#import <UIImageView+WebCache.h>

@interface ZWLLiveViewController ()

@property (weak, nonatomic) UIButton *backBtn;
@property (weak, nonatomic) UIImageView *bigImageView;

@property (nonatomic, strong) IJKFFMoviePlayerController *player;

@property (nonatomic, strong) dispatch_source_t timer;

@property (weak, nonatomic) UIButton *giftBtn;

@end

@implementation ZWLLiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
    
    [self zhiboplay];
    
    [self initTimer];
}

- (void)initTimer {
    
    //初始化心形动画
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(self.timer, ^{
        
        [self showMoreLoveAnimateFromView:self.giftBtn addToView:_player.view];
    });
    dispatch_resume(self.timer);
    
    
}

- (void)zhiboplay
{
    // 设置直播占位图片
    NSURL *imageUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://img.meelive.cn/%@",_live.creator.portrait]];
    [self.bigImageView sd_setImageWithURL:imageUrl placeholderImage:nil];
    
    // 拉流地址
    NSURL *url = [NSURL URLWithString:_live.stream_addr];
    
    // 创建IJKFFMoviePlayerController：专门用来直播，传入拉流地址就好了
    IJKFFMoviePlayerController *playerVc = [[IJKFFMoviePlayerController alloc] initWithContentURL:url withOptions:nil];
    
    // 准备播放
    [playerVc prepareToPlay];
    
    // 强引用，反正被销毁
    _player = playerVc;
    
    playerVc.view.frame = [UIScreen mainScreen].bounds;
    
    [self.view insertSubview:playerVc.view aboveSubview:self.bigImageView];
    [self.view bringSubviewToFront:self.backBtn];
    [self.view bringSubviewToFront:self.giftBtn];
  
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // 界面消失，停止播放
    [_player pause];
    [_player stop];
}

- (void)backBtnClick
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    CGFloat giftW = 40;
    CGFloat giftM = 10;
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 30, 60, 40)];
    [backBtn setTitle:@"返回" forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.view addSubview:backBtn];
    self.backBtn = backBtn;
    
    UIButton *giftBtn = [UIButton new];
    [self.view addSubview:giftBtn];
    giftBtn.frame = CGRectMake(UIScreenW - giftW - giftM, UIScreenH - giftW - giftM, giftW, giftW);
    [giftBtn setBackgroundImage:[UIImage imageNamed:@"mg_room_btn_fenxiang_h"] forState:UIControlStateNormal];
    self.giftBtn = giftBtn;
    
    
    UIImageView *bigImageView = [[UIImageView alloc] init];
    bigImageView.frame = self.view.bounds;
    [self.view addSubview:bigImageView];
    self.bigImageView = bigImageView;
    
}


- (void)showMoreLoveAnimateFromView:(UIView *)fromView addToView:(UIView *)addToView {
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 25)];
    CGRect loveFrame = [fromView convertRect:fromView.frame toView:addToView];
    CGPoint position = CGPointMake(fromView.layer.position.x, loveFrame.origin.y - 30);
    imageView.layer.position = position;
    NSArray *imgArr = @[@"heart_1",@"heart_2",@"heart_3",@"heart_4",@"heart_5",@"heart_1"];
    NSInteger img = arc4random()%6;
    imageView.image = [UIImage imageNamed:imgArr[img]];
    [addToView addSubview:imageView];
    [addToView bringSubviewToFront:imageView];
    
    imageView.transform = CGAffineTransformMakeScale(0.01, 0.01);
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{
        imageView.transform = CGAffineTransformIdentity;
    } completion:nil];
    
    CGFloat duration = 3 + arc4random()%5;
    CAKeyframeAnimation *positionAnimate = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    positionAnimate.repeatCount = 1;
    positionAnimate.duration = duration;
    positionAnimate.fillMode = kCAFillModeForwards;
    positionAnimate.removedOnCompletion = NO;
    
    UIBezierPath *sPath = [UIBezierPath bezierPath];
    [sPath moveToPoint:position];
    CGFloat sign = arc4random()%2 == 1 ? 1 : -1;
    CGFloat controlPointValue = (arc4random()%50 + arc4random()%100) * sign;
    [sPath addCurveToPoint:CGPointMake(position.x, position.y - 300) controlPoint1:CGPointMake(position.x - controlPointValue, position.y - 150) controlPoint2:CGPointMake(position.x + controlPointValue, position.y - 150)];
    positionAnimate.path = sPath.CGPath;
    [imageView.layer addAnimation:positionAnimate forKey:@"heartAnimated"];
    
    
    [UIView animateWithDuration:duration animations:^{
        imageView.layer.opacity = 0;
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
    }];
}


@end
